package com.server.llh.llhserverwork;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.family.llh.lelaohuitoastview.dialog.toast.T;
import com.lelaohui.net.listener.NetDataListener;
import com.lelaohui.net.netmanager.INetRequestManager;
import com.lelaohui.net.netmanager.NetRequestManager;
import com.lelaohui.net.request.LRequest;
import com.lelaohui.net.utils.UrlsTools;
import com.llh.llhsuppliers.R;
import com.server.llh.llhserverwork.activity.base.LlhBaseActivity;
import com.server.llh.llhserverwork.activity.food.LlhFoodServerTitleActivity;
import com.server.llh.llhserverwork.activity.query.QueryUserCardInfoActivity;
import com.server.llh.llhserverwork.activity.server.LlhServerActivityActivity;
import com.server.llh.llhserverwork.activity.work.MySerTaskActivity;
import com.server.llh.llhserverwork.activity.work.QiangDanActivity;
import com.server.llh.llhserverwork.adapter.HomeNewAdatpter;
import com.server.llh.llhserverwork.adapter.VisitNewAdapter;
import com.server.llh.llhserverwork.app.LelaohuirelativesApp;
import com.server.llh.llhserverwork.bean.Constants;
import com.server.llh.llhserverwork.utils.Constant;
import com.server.llh.llhserverwork.utils.ServerPort;
import com.server.llh.llhserverwork.utils.SharedPreferenceUtil;
import com.server.llh.llhserverwork.utils.ToastTool;
import com.server.llh.llhserverwork.view.SlideShowViewPager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import baidu.location.service.view.LocationService;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends LlhBaseActivity {
    private static final String TAG = "MainActivity";
    @BindView(R.id.titleName)
    TextView titleName;
    @BindView(R.id.main_back)
    ImageButton mainBack;
    @BindView(R.id.leftBtn_RL_title_base)
    RelativeLayout leftBtnRLTitleBase;
    @BindView(R.id.serverName)
    TextView serverName;
    @BindView(R.id.linearLayout1)
    LinearLayout linearLayout1;
    SlideShowViewPager slideshowView;
    ViewPager vp;
    @BindView(R.id.indicator_container)
    LinearLayout indicatorContainer;
    @BindView(R.id.llh_unline)
    ImageView llhUnline;
    @BindView(R.id.photo)
    LinearLayout photo;
    @BindView(R.id.food)
    LinearLayout food;
    @BindView(R.id.service)
    LinearLayout service;

    VisitNewAdapter<View> adapter = null;
    GridView gv;
    HomeNewAdatpter homeAdapterr;

    private SparseArray<View> data = new SparseArray<View>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        titleName.setText("乐老汇");
        mainBack.setVisibility(View.INVISIBLE);
        LayoutInflater li = LayoutInflater.from(getApplicationContext());
        gv = (GridView) li.inflate(R.layout.home_page_item, null);
        homeAdapterr = new HomeNewAdatpter(getApplicationContext(), 2);
        gv.setAdapter(homeAdapterr);
        data.put(0, gv);
        adapter = new VisitNewAdapter<View>(data);
        if (vp == null) {
            vp = (ViewPager) findViewById(R.id.viewpager1);
        }
        vp.setAdapter(adapter);
        indicatorContainer = (LinearLayout) this
                .findViewById(R.id.indicator_container);
        ImageView indicator = null;
        for (int i = 0; i < 2; i++) {
            indicator = new ImageView(getApplicationContext());
            indicator.setPadding(0, 0, 10, 0);
            if (i == 0) {
                indicator.setImageResource(R.drawable.point_inactive);
            } else {
                indicator.setImageResource(R.drawable.point_active);
            }
            indicatorContainer.addView(indicator, i);
        }
        vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageSelected(int arg0) {
                if (data.size() > 1) {
                    for (int i = 0; i < indicatorContainer.getChildCount(); i++) {
                        ((ImageView) indicatorContainer.getChildAt(i))
                                .setImageResource(R.drawable.point_active);
                    }
                    ((ImageView) indicatorContainer.getChildAt(arg0))
                            .setImageResource(R.drawable.point_inactive);
                }
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });
        setSlideView();
        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = null;
                switch (position) {
                    case 0:
                        break;
                    case 1:
                        intent = new Intent(getApplicationContext(), LlhFoodServerTitleActivity.class);
                        intent.putExtra("intentTitle", "在线订餐");
                        intent.putExtra("intentTag", "1");
                        intent.putExtra("longtitude", lontitude);
                        intent.putExtra("latitude", latitude);
                        break;
                    case 2:
                        intent = new Intent(getApplicationContext(), LlhServerActivityActivity.class);
                        intent.putExtra("intentTag", "0");
                        break;
                    case 3:
                        break;
                }
                if (intent != null) {
                    startActivity(intent);
                } else {
                    T.showAnimToast(getApplicationContext(), "拼命开发中，敬请期待！");
                }
            }
        });
    }

    private List<Map<String, String>> imageList = new ArrayList<Map<String, String>>();

    private void setSlideView() {
        slideshowView = (SlideShowViewPager) findViewById(R.id.slideshowView);
        for (int i = 0; i < 8; i++) {
            Map<String, String> image_uri = new HashMap<String, String>();
            image_uri.put("imageUrls", Constant.imageUrls[i]);
            image_uri.put("imageUris", Constant.imageUris[i]);
            imageList.add(image_uri);
        }
        slideshowView.setImageUrls(imageList);
    }

    @Override
    public void showProgress() {

    }

    @Override
    protected INetRequestManager getRequestManager() {
        return NetRequestManager.getInstance();
    }

    @Override
    public void hideProgress() {

    }

    @Override
    protected int getlayout() {
        return R.layout.activity_main_first;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void doBasess() {

    }

    @OnClick({R.id.photo, R.id.food, R.id.service})
    public void onViewClicked(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.photo:
                intent = new Intent(getApplicationContext(), MySerTaskActivity.class);
                break;
            case R.id.food:
//                intent=new Intent(getApplicationContext(), QiangDanActivity.class);
                intent = new Intent(getApplicationContext(), QueryUserCardInfoActivity.class);
                intent.putExtra("intentTag", "0");
                break;
            case R.id.service:
                intent = new Intent(getApplicationContext(), LlhServerActivityActivity.class);
                intent.putExtra("intentTag", "1");
                break;
        }
        if (intent != null) {
            startActivity(intent);
        } else {
            T.showAnimToast(getApplicationContext(), "功能开发中，敬请期待！");
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        locationService.start();
    }

    private LocationService locationService;
    String loginStatus, latitude, lontitude;

    @Override
    protected void onStart() {
        super.onStart();
        // -----------location config ------------
        locationService = ((LelaohuirelativesApp) getApplication()).locationService;
        //获取locationservice实例，建议应用中只初始化1个location实例，然后使用，可以参考其他示例的activity，都是通过此种方式获取locationservice实例的
        locationService.registerListener(mListener);
        //注册监听
        int type = getIntent().getIntExtra("from", 0);
        if (type == 0) {
            locationService.setLocationOption(locationService.getDefaultLocationClientOption());
        } else if (type == 1) {
            locationService.setLocationOption(locationService.getOption());
        }
        locationService.start();
    }

    @Override
    protected void onPause() {
        super.onPause();
        locationService.stop();
    }

    /*****
     * 定位结果回调，重写onReceiveLocation方法，可以直接拷贝如下代码到自己工程中修改
     */
    private BDLocationListener mListener = new BDLocationListener() {

        @Override
        public void onReceiveLocation(BDLocation location) {
            // TODO Auto-generated method stub
            if (null != location && location.getLocType() != BDLocation.TypeServerError) {
                StringBuffer sb = new StringBuffer(256);
                sb.append(location.getLocTypeDescription());
                sb.append("\nlatitude : ");// 纬度
                sb.append(location.getLatitude());
                sb.append("\nlontitude : ");// 经度
                sb.append(location.getLongitude());
                latitude = location.getLatitude() + "";
                lontitude = location.getLongitude() + "";
                Log.d(TAG, "loginStatus==" + loginStatus);
//                ToastTool.showText(getApplicationContext(), sb.toString());
            }
        }

        public void onConnectHotSpotMessage(String s, int i) {
        }
    };


    /***
     * Stop location service
     */
    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        locationService.unregisterListener(mListener); //注销掉监听
        locationService.stop(); //停止定位服务
        super.onStop();
    }
}
